package org.asg.dis.controller;

import org.asg.dis.model.*;
import org.asg.dis.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

//TODO 其中营业厅系统管理员拥有所有权限
// 管理员可以方便地添加、删除、修改以及查询网点的信息。
// 另外，管理员还应该可以查看快件总数、问题件总数进行信息的统计汇总。
// 系统管理员与网点负责人对用户的投诉可以做出处理与回复。


@RestController
@RequestMapping("/admin")
@PreAuthorize("hasRole('Admin')")
public class AdminController {

    private final ExpressStationService expressStationService; // 网点服务
    private final ShipmentService shipmentService; // 快件服务
    private final ComplaintService complaintService;
    private final ComplaintResponseService complaintResponseService;

    @Autowired
    public AdminController(ExpressStationService expressStationService, ShipmentService shipmentService, ComplaintService complaintService, ComplaintResponseService complaintResponseService) {
        this.expressStationService = expressStationService;
        this.shipmentService = shipmentService;
        this.complaintService = complaintService;
        this.complaintResponseService = complaintResponseService;
    }

    // 添加网点
    @PostMapping("/es/add")
    public ApiResponse<?> addExpressStation(@RequestBody ExpressStation station) {
        expressStationService.addExpressStation(station);
        return ApiResponse.ok("网点添加成功");
    }

    // 修改网点
    @PutMapping("/es/modify/{id}")
    public ApiResponse<?> modifyExpressStation(@PathVariable("id") Long id, @RequestBody ExpressStation station) {
        expressStationService.modifyExpressStation(id, station);
        return ApiResponse.ok("网点修改成功");
    }

    // 删除网点
    @DeleteMapping("/es/delete/{id}")
    public ApiResponse<?> deleteExpressStation(@PathVariable("id") Long id) {
        expressStationService.deleteExpressStation(id);
        return ApiResponse.ok("网点删除成功");
    }

    // 查询所有网点
    @GetMapping("/escount")
    public ApiResponse<?> getAllExpressStations() {
        List<ExpressStation> stations = expressStationService.getAll();
        return ApiResponse.ok(stations);
    }

    // 查询快件总数
    @GetMapping("/shipment/totalCount")
    public ApiResponse<?> getTotalShipmentCount() {
        long totalCount = shipmentService.getTotalShipmentCount();
        return ApiResponse.ok(totalCount);
    }

    // 查询问题件总数
    @GetMapping("/shipment/problemCount")
    public ApiResponse<?> getProblemShipmentCount() {
        long problemCount = shipmentService.getProblemShipmentCount();
        return ApiResponse.ok(problemCount);
    }

    // 查询未处理投诉
    @GetMapping("complaint/new")
    public ApiResponse<List<Complaint>> getNewComplaint(Long stationId){
         complaintService.getNewByStationId(stationId);
        return ApiResponse.ok();
    }

    // 添加员工
    @PostMapping("/s/add")
    public ApiResponse<?> addStuff(@RequestBody ExpressStation station) {
        expressStationService.addExpressStation(station);

        return ApiResponse.ok("添加成功");
    }

    // 修改员工信息
    @PutMapping("/s/modify/{id}")
    public ApiResponse<?> modifyStuff(@PathVariable("id") Long id, @RequestBody ExpressStation station) {
        expressStationService.modifyExpressStation(id, station);
        return ApiResponse.ok("修改成功");
    }

    // 删除员工
    @DeleteMapping("/s/delete/{id}")
    public ApiResponse<?> deleteStuff(@PathVariable("id") Long id) {
        expressStationService.deleteExpressStation(id);
        return ApiResponse.ok("删除成功");
    }

    // 查看所有投诉
    @GetMapping("/complaints")
    public ApiResponse<?> getAllComplaints() {
        return ApiResponse.ok();
    }

    // 回复投诉
    @PostMapping("/complaint/respond/{complaintId}")
    public ApiResponse<?> respondToComplaint(@PathVariable("complaintId") Long complaintId, @RequestBody ComplaintResponse response){
        complaintResponseService.save(response);

        Complaint complaint = response.getComplaint();  // 获取投诉信息
        String messageContent = "尊敬的客户，您的投诉已处理，回复内容如下：\n" + response.getDetail();
        String actionUrl = "/complaint/details/" + complaintId;  // 假设客户可以通过链接查看详细信息

        Message message = new Message(
                MessageType.NOTIFICATION,            // 消息类型，假设有一个消息类型 "RESPONSE"
                complaint.getShipmentId(),       // 获取运单ID，假设Complaint表有此字段
                "投诉已处理",                      // 消息标题
                messageContent,                  // 消息内容
                response.getProcessor().getUserId(), // 发送者为处理此投诉的用户
                complaint.getUserId(),                // 接收者为投诉发起的客户
                MessageStatus.UNREAD,            // 默认设置消息状态为 "未读"
                LocalDateTime.now()             // 发送时间为当前时间
        );
        message.setActionUrl(actionUrl);
//        messageProducer.sendMessage(message);

        return ApiResponse.ok("回复成功");
    }
}